﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using System.Web.Security;
namespace EC21_EDiamond
{
    public partial class DangKy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDanhSachTinh();
            }
        }
        void LoadDanhSachTinh()
        {
            TinhBUS tinhbus = new TinhBUS();
            drpTinh.DataSource = tinhbus.LoadDanhSachTinh();
            drpTinh.DataTextField = "TenTinh";
            drpTinh.DataValueField = "MaTinh";
            drpTinh.DataBind();
        }
        protected void btnDongY_Click(object sender, EventArgs e)
        {
            try
            {
                //tao tai khoan user
                MembershipCreateStatus p = MembershipCreateStatus.Success;
                Membership.CreateUser(txtTaiKhoan.Text, txtMatKhau.Text, txtEmail.Text, null, null, true, out p);
                lblThongBao.Text = p.ToString();
                if (lblThongBao.Text == "Success")
                {
                    lblThongBao.Text = "";
                    //them khach hang
                    aspnet_UsersBUS userbus = new aspnet_UsersBUS();
                    KhachHangDTO kh = new KhachHangDTO();
                    kh.TenKH = txtTenUser.Text;
                    kh.Useid = Guid.Parse(userbus.LayUserID(txtTaiKhoan.Text));
                    kh.DiaChi = txtDiaChi.Text;
                    kh.MaTinh = int.Parse(drpTinh.SelectedValue);
                    kh.SoDT = txtDienThoai.Text;
                    kh.Email = txtEmail.Text;
                    kh.DiemTichLuy = 0;
                    kh.Tinhtrang = 1;
                    KhachHangBUS khbus = new KhachHangBUS();
                    if (khbus.ThemKhachHang(kh) != 0)
                    {
                        {
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Đăng Ký Thành Công!');", true);
                            txtTaiKhoan.Text = "";
                            txtMatKhau.Text = "";
                            txtNhapLaiMatKhau.Text = "";
                            txtTenUser.Text = "";
                            txtDiaChi.Text = "";
                            txtDienThoai.Text = "";
                            txtEmail.Text = "";
                            lblThongBao.Text = "";
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Đăng Ký Thất Bại!');", true);
                    }
                }
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Đăng Ký Thất Bại!');", true);
            }
        }
    }
}